﻿CREATE PROCEDURE [dbo].[proc_Customer_CallOnTongJi_TelView]
(
	@DepartmentId int,
	@GenjinId int,
	@StartDate nvarchar(10),
	@EndDate nvarchar(10),
	@StartIndex int,
	@EndIndex int
)
As
Begin
	Declare @sRq Datetime,@eRq Datetime
	Set @sRq = Convert(Datetime,@StartDate+' 00:00:00')
	Set @eRq = Convert(Datetime,@EndDate+' 23:59:59')

	If @GenjinId = 1
	Begin
		;WITH list As (Select ROW_NUMBER() OVER (ORDER BY ct.UpTime DESC)AS Row,
			ct.Id,
			ct.CustomerId,
			ct.Record,
			ct.UpTime,
			ct.UpName,
			ct.IntentionsId,
			ct.PublicityDemandId,
			ct.NetworkConsciousnessId,
			ct.GenJinId,
			(Select EName From Employee Where UserName=ct.UpName) As EName,
			(Select CustomerName From Customer Where CustomerId=ct.CustomerId) as CustomerName,
			(Select Title From Customer_Intentions Where Id=ct.IntentionsId) as Intentions,
			(Select Phases From Customer_PublicityDemand Where Id=ct.PublicityDemandId) as PublicityDemand,
			(Select Title From Customer_NetworkConsciousness Where Id=ct.NetworkConsciousnessId) as NetworkConsciousness,
			(Select Title from Customer_genjin Where Id=ct.GenjinId) as GenJin
		From Customer_Track ct
		Where ct.DepartmentId=@DepartmentId and ct.GenJinId In (1,6) and UpTime>=@sRq And UpTime<=@eRq
		)

		Select *,(Select Count(0) From list) As RecordCount From list Where Row Between @StartIndex And @EndIndex
	End
	Else
	Begin
		;WITH list As (Select ROW_NUMBER() OVER (ORDER BY ct.UpTime DESC)AS Row,
			ct.Id,
			ct.CustomerId,
			ct.Record,
			ct.UpTime,
			ct.UpName,
			ct.IntentionsId,
			ct.PublicityDemandId,
			ct.NetworkConsciousnessId,
			ct.GenJinId,
			(Select EName From Employee Where UserName=ct.UpName) As EName,
			(Select CustomerName From Customer Where CustomerId=ct.CustomerId) as CustomerName,
			(Select Title From Customer_Intentions Where Id=ct.IntentionsId) as Intentions,
			(Select Phases From Customer_PublicityDemand Where Id=ct.PublicityDemandId) as PublicityDemand,
			(Select Title From Customer_NetworkConsciousness Where Id=ct.NetworkConsciousnessId) as NetworkConsciousness,
			(Select Title from Customer_genjin Where Id=ct.GenjinId) as GenJin
		From Customer_Track ct
		Where ct.DepartmentId=@DepartmentId and ct.GenJinId=@GenjinId and UpTime>=@sRq And UpTime<=@eRq
		)

		Select *,(Select Count(0) From list) As RecordCount From list Where Row Between @StartIndex And @EndIndex
	End
End
